package com.yunhe.spring.mybatis.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunhe.spring.mybatis.dao.UserMapper;
import com.yunhe.spring.mybatis.model.Page;
import com.yunhe.spring.mybatis.model.User;
import com.yunhe.spring.mybatis.service.IUserService;


@Service("userService")
public class UserServiceImpl implements IUserService{
	
	private static Logger logger = LogManager.getLogger(UserServiceImpl.class);
	
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User getUser(User user) {
		return userMapper.getUser(user);
	}

	@Override
	public void addUser(User user) {
		logger.error("添加用户：" + user);
		userMapper.addUser(user);
	}

	@Override
	public void delUser(User user) {
		logger.error("删除用户：" + user);
		userMapper.delUser(user);
	}

	@Override
	public void editUser(User user) {
		logger.error("更改用户：" + user);
		userMapper.editUser(user);
	}

	@Override
	public Page queryLikeUsers(Map<String, Object> cond) {
		Page page = new Page();
		//根据条件查询符合的用户列表记录总数，赋值给page的totalNum变量
		page.setTotalNum(userMapper.getLikeUsersCount(cond));
		
		//从请求获取当前的页码,并将其赋给page。
		int curPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
		page.setCurPageNum(curPageNum);
		
		//从请求获取参数中每页要显示的数据大小,并将其赋给page
		int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
		page.setPageSize(pageSize);
		
		//动态计算总页数
		page.setTotalPage(page.getTotalNum()/pageSize +(page.getTotalNum()%pageSize==0?0:1));
		
		//根据条件查询符合的用户列表记录，赋给page的result变量
		page.setResult(userMapper.getLikeUsers(cond));
		return page;
	}


}
